package com.tarena.dubbo.order.web.service;

import com.tarena.dubbo.order.web.pojo.Order;
import com.tarena.dubbo.stock.api.StockApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class OrderService {
    //使用这个接口
    @DubboReference //专门给consumer的 注解
    private StockApi stockApi;
    public void createOrder(Order order) {
        //dubbo会扫描到DubboReference后创建代理对象 proxy
        //类似的逻辑在持久层接口mapper 是相同
        System.out.println(stockApi.getClass().getName());
        boolean result = stockApi.reduceStock(order.getProductId(), order.getCount());
        log.info("订单调用减库存的结果是:{}",result);
        log.info("创建订单数据:{},创建订单成功",order);
    }

    public void closeOrder(Order order) {
        log.info("订单关闭:{}",order);
        stockApi.returnStock(order.getProductId(), order.getCount());
    }
}
